package com.mdd.front.controller;

import com.mdd.common.aop.NotLogin;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.competition.CompetitionMatchDetail;
import com.mdd.common.validator.annotation.IDMust;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.ICompetitionService;
import com.mdd.front.validate.article.ArticleCollectValidate;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.competition.CompetitionApplyAcceptValidate;
import com.mdd.front.validate.competition.CompetitionApplyValidate;
import com.mdd.front.validate.competition.CompetitionSearchValidate;
import com.mdd.front.validate.competition.CompetitionSessionValidate;
import com.mdd.front.vo.competition.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/competition")
@Api(tags = "赛事列管理")
public class CompetitionController {
    @Resource
    ICompetitionService iCompetitionService;

    @NotLogin
    @GetMapping("/lists")
    @ApiOperation(value="赛事列列表")
    public AjaxResult<PageResult<CompetitionListedVo>> list(@Validated PageValidate pageValidate,
                                                            @Validated CompetitionSearchValidate searchValidate) {
        PageResult<CompetitionListedVo> list = iCompetitionService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @NotLogin
    @GetMapping("/detail")
    @ApiOperation(value="赛事详情")
    public AjaxResult detail(@Validated @IDMust() @RequestParam("id") Integer id,
                                                            @Validated CompetitionSearchValidate searchValidate) {

        Integer userId = LikeFrontThreadLocal.getUserId();

        CompetitionDetailVo detail = iCompetitionService.detail(id, userId);
        return AjaxResult.success(detail);
    }

    @PostMapping("/apply")
    @ApiOperation(value="赛事申请")
    public AjaxResult apply(@Validated @RequestBody CompetitionApplyValidate competitionApplyValidate){
        Integer memberId = LikeFrontThreadLocal.getMemberId();
        competitionApplyValidate.setMemberId(memberId);
        iCompetitionService.apply(competitionApplyValidate);
        return AjaxResult.success();
    }

    @GetMapping("/apply/lists")
    @ApiOperation(value="赛事报名列表详情")
    public AjaxResult applyLists(@Validated PageValidate pageValidate) {
        Integer memberId = LikeFrontThreadLocal.getMemberId();
        PageResult<CompetitionApplyListsVo> page = iCompetitionService.applyLists(memberId,pageValidate);
        return AjaxResult.success(page);
    }

    @PostMapping("/apply/cancel")
    @ApiOperation(value="报名取消")
    public AjaxResult<Object> applyCancel(@Validated @RequestBody ArticleCollectValidate collectValidate) {
        Integer articleId = collectValidate.getId();

        iCompetitionService.applyCancel(articleId);
        return AjaxResult.success();
    }

    @GetMapping("/apply/invite/{code}")
    @ApiOperation(value="赛事报名邀请")
    public AjaxResult applyInvite(@PathVariable String code) {
        CompetitionApplyInviteVo invite = iCompetitionService.applyInvite(code);
        return AjaxResult.success(invite);
    }

    @PostMapping("/apply/accept")
    @ApiOperation(value="报名邀请接受")
    public AjaxResult<Object> applyAccept(@Validated @RequestBody CompetitionApplyAcceptValidate applyAcceptValidate) {
        Integer memberId = LikeFrontThreadLocal.getMemberId();
        applyAcceptValidate.setMemberId(memberId);
        iCompetitionService.applyAccept(applyAcceptValidate);
        if(applyAcceptValidate.getType()==1) {
            return AjaxResult.success("接受邀请成功！");
        }else{
            return AjaxResult.failed("拒绝邀请成功！");
        }
    }

    @GetMapping("/apply/show")
    public AjaxResult<Object> applyShow(@RequestParam("id") Integer id){
        Integer memberId = LikeFrontThreadLocal.getMemberId();
        CompetitionShowVo data = iCompetitionService.applyShow(memberId,id);
        return AjaxResult.success(data);
    }

//    @Log(title = "赛事场次顺序")
    @NotLogin
    @GetMapping("/session")
    @ApiOperation(value="赛事场次顺序")
    public AjaxResult<CompetitionSessionListedVo> session(@Validated CompetitionSessionValidate sessionValidate) {
        CompetitionSessionListedVo session = iCompetitionService.session(sessionValidate);
        return AjaxResult.success(session);
    }

    @NotLogin
    @GetMapping("/play")
    @ApiOperation(value="赛事场次顺序")
    public AjaxResult play(@RequestParam Integer id,@RequestParam Integer matchId) {
        CompetitionPlayVo play = iCompetitionService.play(id,matchId);
        return AjaxResult.success(play);
    }

    @NotLogin
    @GetMapping("/play/detail")
    @ApiOperation(value="赛事场次顺序")
    public AjaxResult playDetail(@RequestParam Integer matchId) {
        CompetitionMatchDetail play = iCompetitionService.playDetail(matchId);
        return AjaxResult.success(play);
    }
}
